import {diffCycleDays} from './util'

export default function({tapDay, today, funButtons, record, caseKey}){
  const $$ = wx.calendarInst.getDateHandle
  const that = this
  const _ = this._
  const key = caseKey.split('_')[0]
  record = this._getItem(key);

  function resetSalary(){
    const pitsList = that.getElementsById('pits-list')
    const oldDate = pitsList.find({caseKey: key})
    if (oldDate && (oldDate.length||oldDate.data.length)) {
      const prevDate = oldDate.data[0].data.date
      if (prevDate) {
        const prevDateInst = $$(prevDate)
        prevDateInst && prevDateInst.removeClass(key)
      }
    }
    
    pitsList.delete({caseKey: 'salary'})
    const timestr = tapDay.timestr
    const currentDayInst = $$(timestr)
    currentDayInst && currentDayInst.addClass(key)
  }

  funButtons.dot = funButtons.dot.map(item=>{
    if (item.aim) {
      item.aim = item.aim + '?key='+key
    }
    return item
  })

  let {diffDays} = diffCycleDays(tapDay, today)
  tapDay.diffDays = diffDays

  const title = `每月${tapDay.day}日`
  // const content = {caseKey: 'salary', date: tapDay.timestr, body: [title], itemClass: 'salary', ...funButtons}
  const content = {body: [`${diffDays}`, title, '发工资'], caseKey: 'salary', date: tapDay.timestr, itemClass: 'salary', targetDay: tapDay, ...funButtons}
  
  function beforeCallback({createCaseCard}){
    if (record) {
      const date = record.date
      const dateObj = _.getYmd(date)
      let   title = []
      let diffDay = dateObj.day - today.day
      if (diffDay > 0) {
        const lDate = _.lunar.solar2lunar(today.year, today.month, today.day)
        title = [
          '距离发工资还有', 
          {title: `${diffDay}`, itemClass: 'inner-content-case-title-day'}, 
          `那天是${lDate.ncWeek}`
        ]
      } else if(diffDay === 0) {
        title = ['今天发工资', '😎']
      } else {
        diffDay = dateObj.day + (dateObj.days - dateObj.day + diffDay)
        const nextDate = today.offset(diffDay)
        const lDate = _.lunar.solar2lunar(nextDate.year, nextDate.month, nextDate.day)
        title = [
          '距离发工资还有', 
          {title: `${diffDay}`, itemClass: 'inner-content-case-title-day'}, 
          `那天是${lDate.ncWeek}`
        ]
      }
      wx.modalx.showModal({
        content: {
          title, 
          body: [
            `薪水日为每月 ${dateObj.day} 号`, 
            `当前日期：${today.timestr}${tapDay.timestr === today.timestr ? '(今天)':''}`,
            `点击<<重设>>切换薪水日每月 ${tapDay.day} 号`, 
          ],
          itemClass: 'inner-content',
        },
        confirmText: '重设',
        cancelText: '取消', 
        height: 60,
        width: 80,
        itemClass: 'popup salary', 
        mask: false,
        success(res){
          if (res.confirm) {
            resetSalary()
            createCaseCard(content)
          }
          that.restoreCaseButton()
        }
      })
    } else {
      wx.modalx.showModal({
        content: {
          title: [' ', '￥'], 
          body: [
            // `薪水日为每月 ${dateObj.day} 号`, 
            `点击<<设置>>切换薪水日每月 ${tapDay.day} 号，当前选定日期 ${tapDay.timestr}${tapDay.timestr === today.timestr ? '(今天)':''}`, 
          ],
          itemClass: 'inner-content',
        },
        confirmText: '设置',
        cancelText: '取消', 
        height: 60,
        width: 80,
        itemClass: 'popup salary', 
        mask: false,
        success(res){
          if (res.confirm) {
            resetSalary()
            createCaseCard(content)
          }
          that.restoreCaseButton()
        }
      })
    }
  }

  return {key, content, beforeCallback}
}